﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DacSanMienTayOBJ;
public partial class UserControl_ManagerAdvertising : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void InsertNewAdvertising()
    {
        try
        {
            string advName = Request["txtAdvName"];
            int advPositionID = int.Parse(Request["cbPosition"]);
            string link = Request["txtLink"];
            string note = Request["txtNote"];
            string imageLogo = "";
            if (fileLogo.FileName != "")
            {
                string image = Server.MapPath("~/ImageUpload/" + fileLogo.FileName);
                fileLogo.PostedFile.SaveAs(image);
                imageLogo = "ImageUpload/" + fileLogo.FileName;
            }

            Advertising adv = new Advertising();
            adv.AdvName = advName;
            adv.AdvPositionID = advPositionID;
            adv.Logo = imageLogo;
            adv.Link = link;
            adv.Note = note;
            adv.CreateDate = DateTime.Now;
            adv.ExpireDate = DateTime.Now;
            adv.IsVisible = 1;
            adv.Priority = 1;

            bool rs = AdvertisingBusiness.InsertAdvertising(adv, ConnectionType.AdminConnection);
            if (rs)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Đã thêm quảng cáo')", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Không thêm được quảng cáo. Vui lòng liên hệ admin')", true);
            }
        }
        catch
        {

        }
    }
    protected void cmdAdd_Click(object sender, EventArgs e)
    {
        InsertNewAdvertising();
    }

    private void UpdateAdvertising()
    {
        try
        {
            int advID = int.Parse(Request["hdAdvID"]);
            Advertising adv = AdvertisingBusiness.GetAdvertisingDetail(advID, ConnectionType.AdminConnection);
            string advName = Request["txtAdvName"];
            int advPositionID = int.Parse(Request["cbPosition"]);
            string link = Request["txtLink"];
            string note = Request["txtNote"];
            string imageLogo = adv.Logo;
            if (fileLogo.FileName != "")
            {
                string image = Server.MapPath("~/ImageUpload/" + fileLogo.FileName);
                fileLogo.PostedFile.SaveAs(image);
                imageLogo = "ImageUpload/" + fileLogo.FileName;
            }

            bool rs = AdvertisingBusiness.UpdateAdvertising(advID, advName, advPositionID, imageLogo, link, note, ConnectionType.AdminConnection);
            if (rs)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Đã cập nhật xong')", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Không cập nhật được quảng cáo. Vui lòng liên hệ admin')", true);
            }
        }
        catch
        {

        }
    }

    protected void cmdUpdate_Click(object sender, EventArgs e)
    {
        UpdateAdvertising();
    }
}